package com.example.framework.volley;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.framework.R;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 *  它还可以从本地读取图片版
 */
public class VolleyActivity extends Activity
{
      private TextView tv_title;
      private Button bt_volley_get;
      private Button bt_volley_post;
      private Button bt_volley_getjson;
      private Button bt_volley_imagerequest;
      private Button bt_volley_imageloader;
      private Button bt_volley_networkimageview;
      
      private ImageView iv_volley_result;
      private NetworkImageView iv_volley_networkimagview;
      private TextView tv_volley_result;
      
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_volley);
            
            initView();
            tv_title.setText("Volley");
            
            initListener();
      }
      
      private void initView()
      {
            tv_title = (TextView) findViewById(R.id.tv_title);
            bt_volley_get = (Button) findViewById(R.id.bt_volley_get);
            bt_volley_post = (Button) findViewById(R.id.bt_volley_post);
            bt_volley_getjson = (Button) findViewById(R.id.bt_volley_getjson);
            bt_volley_imagerequest = (Button) findViewById(R.id.bt_volley_imagerequest);
            bt_volley_imageloader = (Button) findViewById(R.id.bt_volley_imageloader);
            bt_volley_networkimageview = (Button) findViewById(R.id.bt_volley_networkimageview);
            
            iv_volley_result = (ImageView) findViewById(R.id.iv_volley_result);
            iv_volley_networkimagview = (NetworkImageView) findViewById(R.id.iv_volley_networkimagview);
            tv_volley_result = (TextView) findViewById(R.id.tv_volley_result);
      }
      
      Response.Listener<String> listener = new Response.Listener<>()
      {
            // 正确接收数据回调
            @Override
            public void onResponse(String s)
            {
                  try
                  {
                        s = new String(s.getBytes("ISO-8859-1"), "UTF-8"); //我加的
                  }
                  catch (UnsupportedEncodingException e)
                  {
                        throw new RuntimeException(e);
                  }
                  tv_volley_result.setText(s);
            }
      };
      
      
      Response.ErrorListener listener2 = new Response.ErrorListener()
      {// 发生异常后的监听回调
            @Override
            public void onErrorResponse(VolleyError volleyError)
            {
                  tv_volley_result.setText("加载失败" + volleyError);
            }
      };
      
      private void initListener()
      {
            // get请求
            bt_volley_get.setOnClickListener(new View.OnClickListener()
            {
                  @Override
                  public void onClick(View v)
                  {
                        
                        // 1 创建一个请求队列
                        RequestQueue requestQueue = Volley.newRequestQueue(VolleyActivity.this);
                        String url = "https://www.baidu.com/";
                        
                        StringRequest stringRequest = new StringRequest( Request.Method.GET, url, listener, listener2);
                        
                        // 3 将创建的请求添加到请求队列中
                        requestQueue.add(stringRequest);
                  }
            });
            
            // post请求
            bt_volley_post.setOnClickListener(new View.OnClickListener()
            {
                  @Override
                  public void onClick(View v)
                  {
                        // 1 创建一个请求队列
                        RequestQueue requestQueue = Volley.newRequestQueue(VolleyActivity.this);
                        
                        // 2 创建一个post请求
                        String url = "http://api.m.mtime.cn/PageSubArea/TrailerList.api";
                        StringRequest stringRequest = new StringRequest(
                                Request.Method.POST, url, new Response.Listener<String>()
                        {
                              @Override
                              public void onResponse(String s)
                              {
                                    tv_volley_result.setText(s);
                              }
                        }, new Response.ErrorListener()
                        {
                              @Override
                              public void onErrorResponse(VolleyError volleyError)
                              {
                                    tv_volley_result.setText("请求失败" + volleyError);
                              }
                        })  //到这里参数结束 , 下面是StringRequest的方法体
                        {
                              @Override
                              protected Map<String, String> getParams() throws AuthFailureError
                              {
                                    
                                    Map<String, String> map = new HashMap<String, String>();
                                    //                        map.put("value1","param1");
                                    
                                    return map;
                              }
                        };
                        
                        // 3 将post请求添加到队列中
                        requestQueue.add(stringRequest);
                  }
            });
            
            // 获取json数据, 分请求对象或为数组
            bt_volley_getjson.setOnClickListener(new View.OnClickListener()
            {
                  @Override
                  public void onClick(View v)
                  {
                        // 1 创建一个请求队列
                        RequestQueue requestQueue = Volley.newRequestQueue(VolleyActivity.this);
                        
                        // 2 创建一个请求
                        String url = "http://api.m.mtime.cn/PageSubArea/TrailerList.api";
                        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( Request.Method.GET,
                                url, null, new Response.Listener<JSONObject>()
                        {
                              @Override
                              public void onResponse(JSONObject jsonObject)
                              {
                                    tv_volley_result.setText(jsonObject.toString());
                              }
                        }, new Response.ErrorListener()
                        {
                              @Override
                              public void onErrorResponse(VolleyError volleyError)
                              {
                                    tv_volley_result.setText("请求失败" + volleyError);
                              }
                        });
                        
                        // 3 将创建的请求添加到请求队列中
                        requestQueue.add(jsonObjectRequest);
                  }
            });
            
            // imagerequest加载图片
            bt_volley_imagerequest.setOnClickListener(new View.OnClickListener()
            {
                  @Override
                  public void onClick(View v)
                  {
                        // 1 创建一个请求队列
                        RequestQueue requestQueue = Volley.newRequestQueue(VolleyActivity.this);
                        
                        // 2 创建一个图片的请求
                        String url = "https://www.gfxcamp.com/wp-content/uploads/2025/03/Autodesk-3DS-MAX-2026.jpg";
                        
                        ImageRequest imageRequest = new ImageRequest(
                                url, new Response.Listener<Bitmap>()
                        {
                              @Override
                              public void onResponse(Bitmap bitmap)
                              {
                                    // 正确接收到图片
                                    iv_volley_result.setVisibility(View.VISIBLE);
                                    iv_volley_result.setImageBitmap(bitmap);
                              }
                        }, 0, 0, ImageView.ScaleType.CENTER_INSIDE, Bitmap.Config.RGB_565, new Response.ErrorListener()
                        {
                              @Override
                              public void onErrorResponse(VolleyError volleyError)
                              {
                                    iv_volley_result.setImageResource(R.drawable.atguigu_logo);
                              }
                        });
                        
                        // 3 将请求添加到请求队列中
                        requestQueue.add(imageRequest);
                  }
            });
            
            // imageloader加载图片 , 缓存类
            bt_volley_imageloader.setOnClickListener(new View.OnClickListener()
            {
                  @Override
                  public void onClick(View v)
                  {
                        // 创建一个请求队列
                        RequestQueue requestQueue = Volley.newRequestQueue(VolleyActivity.this);
                        
                        // 创建一个imageloader
                        //                ImageLoader imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() {
                        //                    @Override
                        //                    public Bitmap getBitmap(String s) {
                        //                        return null;
                        //                    }
                        //
                        //                    @Override
                        //                    public void putBitmap(String s, Bitmap bitmap) {
                        //
                        //                    }
                        //                });
                        ImageLoader imageLoader = new ImageLoader(requestQueue, new BitmapCache());
                        
                        // 加载图片
                        String url = "https://www.gfxcamp.com/wp-content/uploads/2024/10/GeoGen.jpg";
                        iv_volley_result.setVisibility(View.VISIBLE);
                        ImageLoader.ImageListener imageListener = imageLoader.getImageListener(iv_volley_result, R.drawable.atguigu_logo, R.drawable.atguigu_logo);
                        imageLoader.get(url, imageListener);
                  }
            });
            
            // networkimageview加载图片
            bt_volley_networkimageview.setOnClickListener(new View.OnClickListener()
            {
                  @Override
                  public void onClick(View v)
                  {
                        
                        // 让控件显示
                        iv_volley_networkimagview.setVisibility(View.VISIBLE);
                        
                        // 创建一个请求队列
                        RequestQueue requestQueue = Volley.newRequestQueue(VolleyActivity.this);
                        
                        // 创建一个Imageloader
                        ImageLoader imageLoader = new ImageLoader(requestQueue, new BitmapCache());
                        
                        // 默认图片和异常图片设置
                        iv_volley_networkimagview.setDefaultImageResId(R.drawable.atguigu_logo);
                        iv_volley_networkimagview.setErrorImageResId(R.drawable.atguigu_logo);
                        
                        // 加载图片
                        String url = "https://www.gfxcamp.com/wp-content/uploads/2024/11/VRay-7-For-3DS-MAX-2020-2025.jpg";
                        iv_volley_networkimagview.setImageUrl(url, imageLoader);
                  }
            });
      }
      
      
}
